/*
//
//              INTEL CORPORATION PROPRIETARY INFORMATION
//  This software is supplied under the terms of a license  agreement or
//  nondisclosure agreement with Intel Corporation and may not be copied
//  or disclosed except in  accordance  with the terms of that agreement.
//    Copyright (c) 2007 Intel Corporation. All Rights Reserved.
//
//
*/

#include "umc_defs.h"
#if defined(UMC_ENABLE_AVS_VIDEO_DECODER)

#include "umc_avs_dec_slice_init.h"
#include "umc_avs_dec_bit_stream.h"
#include "umc_avs_slice.h"
#include "umc_avs_sequence_header.h"
#include "umc_avs_picture_header.h"
#include "umc_avs_context.h"

namespace UMC
{

Status InitializeSlice(AVSSlice *pSlice,
                       const Ipp32u *pSrc,
                       size_t srcSize,
                       const AVS_SEQUENCE_HEADER *pSeqHeader,
                       const AVS_PICTURE_HEADER *pPicHeader)
{
    AVS_BIT_STREAM_CONTEXT ctx;
    AVS_SLICE_HEADER *pSlcHeader;
    AVS_DECODING_CONTEXT *pDecCtx;

    // check error(s)
    if ((NULL == pSrc) ||
        (NULL == pSeqHeader) ||
        (NULL == pPicHeader))
    {
        return UMC_ERR_NULL_PTR;
    }
    if ((VIDEO_SEQUENCE_START_CODE != pSeqHeader->video_sequence_start_code) &&
        (I_PICTURE_START_CODE != pPicHeader->picture_start_code) &&
        (BP_PICTURE_START_CODE != pPicHeader->picture_start_code))
    {
        return UMC_ERR_INVALID_PARAMS;
    }

    // reset pointers
    pSlcHeader = &(pSlice->m_SlcHeader);
    pDecCtx = &(pSlice->m_decCtx);

    // initialize bit stream context
    InitializeDecBitStream(&ctx, pSrc, srcSize);

    // decode slice header
    if (false == DecodeSlcHeader(pSlcHeader, &ctx, pSeqHeader, pPicHeader))
        return UMC_ERR_INVALID_STREAM;
    if (0 > GetBitsLeftToDecode(&ctx))
    {
         return UMC_ERR_FAILED;
    }

    // reset decoding context
    memset(pDecCtx, 0, sizeof(AVS_DECODING_CONTEXT));
    pDecCtx->m_stream = ctx;

    return UMC_OK;

} // bool InitializeSlice(AVSSlice *pSlice,

} // namespace UMC

#endif // defined(UMC_ENABLE_AVS_VIDEO_DECODER)
